<?php
require_once '/Workerman/Autoloader.php';
use Workerman\Lib\MySQL as Db;

define('DEBUG', true);

function query($name, $param, $return="query") {
    $db = new Db('db.nao.com', '3306', 'lee', '1234', 'naonao');
    $data = $db->call($name, $param)->$return();
    $db->closeConnection();
    return $data;
}

function encode($type, $data) {
    return json_encode(['type' => $type, 'data' => $data]);
}

function formatPhone($phone) {
    return substr($phone, 0, 3).' '.substr($phone, 3, 4).' '.substr($phone, -4);
}

function E($connection, $msg) {
    if (!DEBUG) {
        return;
    }
    $localmsg = "";
    if (!is_null($connection)) {
        $localmsg = $connection->getRemoteIp().':'.$connection->getRemotePort();
        if (isset($connection->user['phone'])) {
            $localmsg = $connection->user['phone'].'('.$localmsg.')';
        }
    }
    echo "[".date("Y-m-d H:i:s")."] ".$localmsg." ".$msg, PHP_EOL;
}
